home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / disk / xdu21dvx.zip / XWIN.C < prev   
C/C++ Source or Header  |  1993-07-27  |  8KB  |  370 lines

  1. /*
  2.  * XDU - X Window System Interface.
  3.  *
  4.  * We hide all of the X hieroglyphics inside of this module.
  5.  *
  6.  * Phillip C. Dykstra
  7.  * <phil@arl.army.mil>
  8.  * 4 Sep 1991.
  9.  * 
  10.  * Copyright (C)    Phillip C. Dykstra    1991, 1993
  11.  * Permission to use, copy, modify, distribute, and sell this software
  12.  * and its documentation for any purpose is hereby granted without fee,
  13.  * provided that the above copyright notice appear in all copies and that
  14.  * both that copyright notice and this permission notice appear in
  15.  * supporting documentation, and that the authors name not be used in
  16.  * advertising or publicity pertaining to distribution of the software
  17.  * without specific, written prior permission.  The author makes no
  18.  * representations about the suitability of this software for any purpose.
  19.  * It is provided "as is" without express or implied warranty.
  20.  */
  21. #include <stdio.h>
  22. #include <X11/Intrinsic.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Core.h>
  25.  
  26. /* IMPORTS: routines that this module vectors out to */
  27. extern int press();
  28. extern int reset();
  29. extern int repaint();
  30. extern int reorder();
  31. extern int setorder();
  32. extern int nodeinfo();
  33. extern int helpinfo();
  34. extern int ncols;
  35.  
  36. /* EXPORTS: routines that this module exports outside */
  37. extern int xsetup();
  38. extern int xmainloop();
  39. extern int xclear();
  40. extern int xrepaint();
  41. extern int xrepaint_noclear();
  42. extern int xdrawrect();
  43.  
  44. static String fallback_resources[] = {
  45. "*window.width:        600",
  46. "*window.height:    480",
  47. "*order:        first",
  48. NULL
  49. };
  50.  
  51. /* Application Resources */
  52. typedef struct {
  53.     Pixel    foreground;
  54.     Pixel    background;
  55.     XFontStruct *font;
  56.     int    ncol;
  57.     Boolean    showsize;
  58.     char    *order;
  59. } res_data, *res_data_ptr;
  60. static res_data res;
  61.  
  62. static XtResource application_resources[] = {
  63.     { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  64.         XtOffset(res_data_ptr,foreground), XtRString, XtDefaultForeground},
  65.     { XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel),
  66.         XtOffset(res_data_ptr,background), XtRString, XtDefaultBackground},
  67.     { XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  68.         XtOffset(res_data_ptr,font), XtRString, XtDefaultFont },
  69.     { "ncol", "Ncol", XtRInt, sizeof(int),
  70.         XtOffset(res_data_ptr,ncol), XtRString, "5"},
  71.     { "showsize", "ShowSize", XtRBoolean, sizeof(Boolean),
  72.         XtOffset(res_data_ptr,showsize), XtRString, "True"},
  73.     { "order", "Order", XtRString, sizeof(String),
  74.         XtOffset(res_data_ptr,order), XtRString, "first"}
  75. };
  76.  
  77. /* Command Line Options */
  78. static XrmOptionDescRec options[] = {
  79.     {"-c",        "*ncol",    XrmoptionSepArg,    NULL},
  80.     {"+s",        "*showsize",    XrmoptionNoArg,        "True"},
  81.     {"-s",        "*showsize",    XrmoptionNoArg,        "False"},
  82.     {"-n",        "*order",    XrmoptionNoArg,        "size"},
  83.     {"-rn",        "*order",    XrmoptionNoArg,        "rsize"},
  84.     {"-a",        "*order",    XrmoptionNoArg,        "alpha"},
  85.     {"-ra",        "*order",    XrmoptionNoArg,        "ralpha"}
  86. };
  87.  
  88. /* action routines */
  89. static void a_goto();
  90. static void a_reset();
  91. static void a_quit();
  92. static void a_reorder();
  93. static void a_size();
  94. static void a_ncol();
  95. static void a_info();
  96. static void a_help();
  97.  
  98. static XtActionsRec actionsTable[] = {
  99.     { "reset",    a_reset },
  100.     { "goto",    a_goto },
  101.     { "quit",    a_quit },
  102.     { "reorder",    a_reorder },
  103.     { "size",    a_size },
  104.     { "ncol",    a_ncol },
  105.     { "info",    a_info },
  106.     { "help",    a_help }
  107. };
  108.  
  109. static char defaultTranslations[] = "\
  110. <Key>Q:    quit()\n\
  111. <Key>Escape: quit()\n\
  112. <Key>/:    reset()\n\
  113. <Key>S:    size()\n\
  114. <Key>I:    info()\n\
  115. <Key>H: help()\n\
  116. <Key>A:    reorder(alpha)\n\
  117. <Key>N:    reorder(size)\n\
  118. <Key>F:    reorder(first)\n\
  119. <Key>L:    reorder(last)\n\
  120. <Key>R:    reorder(reverse)\n\
  121. <Key>1:    ncol(1)\n\
  122. <Key>2:    ncol(2)\n\
  123. <Key>3:    ncol(3)\n\
  124. <Key>4:    ncol(4)\n\
  125. <Key>5:    ncol(5)\n\
  126. <Key>6:    ncol(6)\n\
  127. <Key>7:    ncol(7)\n\
  128. <Key>8:    ncol(8)\n\
  129. <Key>9:    ncol(9)\n\
  130. <Key>0:    ncol(10)\n\
  131. <Btn1Down>: goto()\n\
  132. <Btn2Down>: reset()\n\
  133. <Btn3Down>: quit()\n\
  134. ";
  135.  
  136. /*  action routines  */
  137.  
  138. static void a_quit(w, event, params, num_params)
  139. Widget w;
  140. XEvent *event;
  141. String *params;
  142. Cardinal *num_params;
  143. {
  144.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  145.     exit(0);
  146. }
  147.  
  148. static void a_goto(w, event, params, num_params)
  149. Widget w;
  150. XEvent *event;
  151. String *params;
  152. Cardinal *num_params;
  153. {
  154.     press(event->xbutton.x, event->xbutton.y);
  155. }
  156.  
  157. static void a_reset(w, event, params, num_params)
  158. Widget w;
  159. XEvent *event;
  160. String *params;
  161. Cardinal *num_params;
  162. {
  163.     reset();
  164. }
  165.  
  166. static void a_reorder(w, event, params, num_params)
  167. Widget w;
  168. XEvent *event;
  169. String *params;
  170. Cardinal *num_params;
  171. {
  172.     if (*num_params != 1) {
  173.         fprintf(stderr, "xdu: bad number of params to reorder action\n");
  174.     } else {
  175.         reorder(*params);
  176.     }
  177. }
  178.  
  179. static void a_size(w, event, params, num_params)
  180. Widget w;
  181. XEvent *event;
  182. String *params;
  183. Cardinal *num_params;
  184. {
  185.     if (res.showsize)
  186.         res.showsize = 0;
  187.     else
  188.         res.showsize = 1;
  189.     xrepaint();
  190. }
  191.  
  192. static void a_ncol(w, event, params, num_params)
  193. Widget w;
  194. XEvent *event;
  195. String *params;
  196. Cardinal *num_params;
  197. {
  198.     int    n;
  199.  
  200.     if (*num_params != 1) {
  201.         fprintf(stderr, "xdu: bad number of params to ncol action\n");
  202.         return;
  203.     }
  204.     n = atoi(*params);
  205.     if (n < 1 || n > 1000) {
  206.         fprintf(stderr, "xdu: bad value to ncol action\n");
  207.         return;
  208.     }
  209.     ncols = res.ncol = n;
  210.     xrepaint();
  211. }
  212.  
  213. static void a_info(w, event, params, num_params)
  214. Widget w;
  215. XEvent *event;
  216. String *params;
  217. Cardinal *num_params;
  218. {
  219.     nodeinfo();
  220. }
  221.  
  222. static void a_help(w, event, params, num_params)
  223. Widget w;
  224. XEvent *event;
  225. String *params;
  226. Cardinal *num_params;
  227. {
  228.     helpinfo();
  229. }
  230.  
  231. /* callback routines */
  232.  
  233. static void c_resize(w, data, event)
  234. Widget w;
  235. caddr_t data;
  236. XEvent *event;
  237. {
  238.     /*printf("Resize\n");*/
  239.     xrepaint();
  240. }
  241.  
  242. static void c_repaint(w, data, event)
  243. Widget w;
  244. caddr_t data;
  245. XEvent *event;
  246. {
  247.     /*printf("Expose\n");*/
  248.     xrepaint_noclear();
  249. }
  250.  
  251. /* X Window related variables */
  252. static Cursor WorkingCursor;
  253. static Display *dpy;
  254. static int screen;
  255. static Visual *vis;
  256. static Window win;
  257. static GC gc;
  258. static GC cleargc;
  259. static XtAppContext app_con;
  260.  
  261. /*  External Functions  */
  262.  
  263. int
  264. xsetup(argcp, argv)
  265. int *argcp;
  266. char **argv;
  267. {
  268.     XtTranslations trans_table;
  269.     Widget toplevel;
  270.     Widget w;
  271.     XGCValues gcv;
  272.  
  273.     /* Create the top level Widget */
  274.     toplevel = XtAppInitialize(&app_con, "XDu",
  275.             options, XtNumber(options),
  276.             argcp, argv,
  277.             fallback_resources, NULL, 0);
  278.  
  279.     XtGetApplicationResources(toplevel, (XtPointer)&res,
  280.         application_resources, XtNumber(application_resources),
  281.         NULL, 0 );
  282.  
  283.     XtAppAddActions(app_con, actionsTable, XtNumber(actionsTable));
  284.     trans_table = XtParseTranslationTable(defaultTranslations);
  285.  
  286.     /* Create a simple Core class widget to draw in */
  287.     w = XtCreateManagedWidget("window", coreWidgetClass, toplevel,
  288.         NULL, 0);
  289.  
  290.     /* events */
  291.     XtAddEventHandler(w, ExposureMask, FALSE, c_repaint, NULL);
  292.     XtAddEventHandler(w, StructureNotifyMask, FALSE, c_resize, NULL);
  293.     XtAugmentTranslations(w, trans_table);
  294.  
  295.     XtRealizeWidget(toplevel);
  296.  
  297.     /* We need these for the raw Xlib calls */
  298.     win = XtWindow(w);
  299.     dpy = XtDisplay(w);
  300.     screen = DefaultScreen(dpy);
  301.     vis = DefaultVisual(dpy,screen);
  302.  
  303.     gcv.foreground = res.foreground;
  304.     gcv.background = res.background;
  305.     gcv.font = res.font->fid;
  306.     gc = XCreateGC(dpy, win, (GCFont|GCForeground|GCBackground), &gcv);
  307.  
  308.     setorder(res.order);
  309.     ncols = res.ncol;
  310. }
  311.  
  312. xmainloop()
  313. {
  314.     XtAppMainLoop(app_con);
  315.     return(0);
  316. }
  317.  
  318. xclear()
  319. {
  320.     XClearWindow(dpy, win);
  321. }
  322.  
  323. xrepaint()
  324. {
  325.     XWindowAttributes xwa;
  326.  
  327.     XClearWindow(dpy, win);
  328.     XGetWindowAttributes(dpy, win, &xwa);
  329.     repaint(xwa.width, xwa.height);
  330. }
  331.  
  332. xrepaint_noclear()
  333. {
  334.     XWindowAttributes xwa;
  335.  
  336.     XGetWindowAttributes(dpy, win, &xwa);
  337.     repaint(xwa.width, xwa.height);
  338. }
  339.  
  340. xdrawrect(name, size, x, y, width, height)
  341. char *name;
  342. int size;
  343. int x, y, width, height;
  344. {
  345.     int    textx, texty;
  346.     char    label[1024];
  347.     XCharStruct overall;
  348.     int    ascent, descent, direction;
  349.     int    cheight;
  350.  
  351.     /*printf("draw(%d,%d,%d,%d)\n", x, y, width, height );*/
  352.     XDrawRectangle(dpy, win, gc, x, y, width, height);
  353.  
  354.     if (res.showsize) {
  355.         sprintf(label,"%s (%d)", name, size);
  356.         name = label;
  357.     }
  358.  
  359.     XTextExtents(res.font, name, strlen(name), &direction,
  360.         &ascent, &descent, &overall);
  361.     cheight = overall.ascent + overall.descent;
  362.     if (height < (cheight + 2))
  363.         return;
  364.  
  365.     /* print label */
  366.     textx = x + 4;
  367.     texty = y + height/2.0 + (overall.ascent - overall.descent)/2.0 + 1.5;
  368.     XDrawString(dpy, win, gc, textx, texty, name, strlen(name));
  369. }
  370.